
using System.Web.UI.HtmlControls;
using System;
using System.Configuration;
using System.Drawing;
using System.Data;
using System.Web.UI.WebControls;
using Microsoft.VisualBasic;
using System.Collections;
using System.Web.UI;
using System.Web;


using System.IO;
using System.Text;
//using com.ddsc.database;
using DDSC.OracleADO;
using com.ddsc.common;
using SMAS;
using System.Resources;

namespace smasb2b
{
	public class VNDW0040 : BasePage
	{
		
		//----------------------------------------------------------------------------------------------------------
		//版本  修改日期  修改人員  修改案號    修　改　內　容
		//－－  －－－－  －－－－  －－－－－  －－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－
		//0006  20110915  walter    P3511M0122-報價呈案程式修改案
		//----------------------------------------------------------------------------------------------------------
		
#region  Web Form 設計工具產生的程式碼
		
		//此為 Web Form 設計工具所需的呼叫。
		[System.Diagnostics.DebuggerStepThrough()]private void InitializeComponent()
		{
			
		}
		protected System.Web.UI.WebControls.Label lblENGLISH_SHORTNAME1;
		protected ddscControlLibrary.InputBoxCtrl txtENGLISH_SHORTNAME;
		protected ddscControlLibrary.SubmitButtonCtrl btnSearch;
		protected System.Web.UI.WebControls.Label lblENGLISH_SHORTNAME;
		protected System.Web.UI.WebControls.Label lblCLASS;
		protected System.Web.UI.WebControls.Label lblSTATUS;
		protected System.Web.UI.WebControls.Label lblPAGEINDEX;
		protected ddscControlLibrary.InputBoxCtrl PageIndex;
		protected System.Web.UI.WebControls.Label lblPAGE1;
		protected System.Web.UI.WebControls.Label lblTOTAL;
		protected ddscControlLibrary.InputBoxCtrl PageCount;
		protected System.Web.UI.WebControls.Label lblPAGE;
		protected System.Web.UI.WebControls.ImageButton btnPREV;
		protected ddscControlLibrary.InputBoxCtrl GotoPage;
		protected System.Web.UI.WebControls.ImageButton BtnGo;
		protected System.Web.UI.WebControls.ImageButton BtnNext;
		protected System.Web.UI.HtmlControls.HtmlImage IMG1;
		protected System.Web.UI.HtmlControls.HtmlTable DataGrid;
		protected System.Web.UI.HtmlControls.HtmlTableRow DataGrid_foot;
		protected System.Web.UI.WebControls.Label lblVNDW0040;
		protected System.Web.UI.WebControls.Label lblSUPPLY_PRICE;
		protected ddscControlLibrary.InputBoxCtrl txtCODE;
		protected System.Web.UI.WebControls.Label lblCODE2;
		protected System.Web.UI.WebControls.Label lblCODE3;
		
		//注意: 下列預留位置宣告是 Web Form 設計工具需要的項目。
		//請勿刪除或移動它。
		private System.Object designerPlaceholderDeclaration;
		
		private void Page_Init(System.Object sender, System.EventArgs e)
		{
			//CODEGEN: 此為 Web Form 設計工具所需的方法呼叫
			//請勿使用程式碼編輯器進行修改。
			InitializeComponent();
		}
		
#endregion
		
		private DataTable createErrorTable(string mError)
		{
			dbReturnInfo dbInfo = new dbReturnInfo("info");
			dbInfo.AddNewRow();
			dbInfo.setReturnDataValue("F", System.Convert.ToString(999), 0, mError);
			return dbInfo.getReturnDataTable();
		}


        public void DataGrid_DataBind()
        {
            //變數宣告
            VENDOR LoginUser = GetLoginUser();
            DataSet ds = default(DataSet);
            dbAccess dba = new dbAccess();
            ArrayList m_arrParam = new ArrayList();
            string strCondition = "";
            int i = 0;
            int count = 2;
            string[] str_SQL_a = new string[count + 1];
            string[] str_SQL_b = new string[count + 1];

            try
            {
                //目前頁次指定Goto
                GotoPage.Text = Server.HtmlEncode(PageIndex.Text);

                //預設查詢語法
                //P3511M0122-報價呈案程式修改案
                //Dim SqlCommandText = "select * from (select A.*,  (decode(b.status,'8','8-報價中','2','2-已報價','1','','')) status from (select A.CODE CODE3,B.ENGLISH_SHORTNAME,A.SUPPLY_PRICE from VND_SUPPLY A,BAS_MATERIAL B where A.code=B.code(+)  AND A.VENDOR_NO='" & LoginUser.VENDOR_NO & "') A ,B2B_SUPPLY B where A.CODE3=B.CODE(+) ) "
                //string SqlCommandText = "select * from (select A.*,  (decode(b.status,\'3\',\'3-報價中\',\'4\',\'4-報價中\',\'5\',\'5-報價中\',\'6\',\'6-報價中\',\'7\',\'7-報價中\',\'8\',\'8-報價中\',\'2\',\'2-已報價\',\'1\',\'1-已報價\',\'\')) status from (select A.CODE CODE3,B.ENGLISH_SHORTNAME,A.SUPPLY_PRICE from VND_SUPPLY A,BAS_MATERIAL B where A.code=B.code(+)  AND A.VENDOR_NO=\'" + LoginUser.VENDOR_NO + "\') A ,B2B_SUPPLY B where A.CODE3=B.CODE(+) ) ";
                string SqlCommandText = "select * from (select A.*,  (decode(b.status,\'3\',\'3-報價中\',\'4\',\'4-報價中\',\'5\',\'5-報價中\',\'6\',\'6-報價中\',\'7\',\'7-報價中\',\'8\',\'8-報價中\',\'2\',\'2-已報價\',\'1\',\'1-已報價\',\'\')) status from (select A.CODE CODE3,B.ENGLISH_SHORTNAME,A.SUPPLY_PRICE from VND_SUPPLY A,BAS_MATERIAL B where A.code=B.code(+)  AND A.VENDOR_NO=:VENDORNO) A ,B2B_SUPPLY B where A.CODE3=B.CODE(+) ) ";

                string SqlCommandCount = "SELECT count(*) row_total_count from (" + SqlCommandText + ") ";

                //組合條件字串
                str_SQL_a[0] = txtCODE.Text.Replace(",", "\'\'");
                //str_SQL_b[0] = string.Format("CODE3 = \'{0}\' and ", str_SQL_a[0]);
                str_SQL_b[0] = "CODE3 like :SQLA0 and ";

                str_SQL_a[1] = txtENGLISH_SHORTNAME.Text.Replace(",", "\'\'");
                //str_SQL_b[1] = string.Format("ENGLISH_SHORTNAME like \'{0}%\' and ", str_SQL_a[1]);
                str_SQL_b[1] = "ENGLISH_SHORTNAME like :SQLA1 and ";
                //查詢條件組合
                for (i = 0; i <= count - 1; i++)
                {
                    if (str_SQL_a[i].Trim() != "")
                    {
                        strCondition = strCondition + str_SQL_b[i];
                    }
                }

                m_arrParam.Clear();
                m_arrParam.Add(dba.GetOracleParam(":VENDORNO", LoginUser.VENDOR_NO));

                if (str_SQL_a[0].Trim() != "")
                {
                    m_arrParam.Add(dba.GetOracleParam(":SQLA0", str_SQL_a[0] + "%"));
                }

                if (str_SQL_a[1].Trim() != "")
                {
                    m_arrParam.Add(dba.GetOracleParam(":SQLA1", "%" + str_SQL_a[1] + "%"));

                }


                if (!(String.IsNullOrEmpty(strCondition)))
                {
                    strCondition = " WHERE " + strCondition.Substring(0, strCondition.Length - 5);
                    // strCondition = " where VENDOR_NO='" & LoginUser.VENDOR_NO & "'" + strCondition.Substring(0, strCondition.Length - 3)

                }

                int intCount = 0;
                int size = PageSize; // Update by titan 由BasePage設定

                // 計算資料總筆數
                //DataSet dsCount = dba.runExecuteQuery("Vendor_Count", SqlCommandCount + strCondition);

                DataSet dsCount = dba.runExecuteQuery("Vendor_Count", SqlCommandCount + strCondition, m_arrParam);
                if (dsCount.Tables[0].Rows[0]["ReturnCode"] == "000")
                {
                    intCount = int.Parse(System.Convert.ToString(dsCount.Tables["Vendor_Count"].Rows[0]["row_total_count"]));
                    PageCount.Text = System.Convert.ToString(Math.Ceiling(System.Convert.ToDecimal(double.Parse((intCount).ToString()) / size)));
                }

                //Update By Titan 處理當總頁次小於目前頁次
                if ((int.Parse(PageCount.Text) < int.Parse(PageIndex.Text)) && int.Parse(PageCount.Text) > 0)
                {
                    // 總筆數(intCount)小於一頁，從第0筆開始顯示
                    PageIndex.Text = Server.HtmlEncode(PageCount.Text);
                    GotoPage.Text = Server.HtmlEncode(PageCount.Text);
                }
                // Update by titan 由BasePage設定
                // absolute position
                int position = ((int.Parse(PageIndex.Text)) - 1) * size;
                int maxrecord = position + size;


                //控制分頁按鈕
                ControlPageButton(DataGrid_foot);

                //執行資料庫語法
                ds = dba.runExecuteQuery("VND_SUPPLY", SqlCommandText + strCondition + " ORDER BY CODE3 ", position, maxrecord, m_arrParam);

                if (!(ds == null) && ds.Tables["info"].Rows[0]["ReturnCode"] != "100" && ds.Tables["info"].Rows[0]["ReturnCode"] != "999")
                {
                    HtmlTableRow tr = (HtmlTableRow)(DataGrid.FindControl("DataGrid_head"));
                    ArrayList ArrKey = new ArrayList();
                    ArrKey.Add("CODE3");
                    ArrKey.Add("ENGLISH_SHORTNAME");
                    ArrKey.Add("STATUS");

                    DataGrid_Bind_VNDW0040(DataGrid, ArrKey, ds.Tables["VND_SUPPLY"]);

                }
                else
                {
                    ResourceManager rm = GetResourceManager();
                    showException(rm.GetString("MSG069"));
                }

            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                ds.Dispose();
            }
        }
		
		private void BtnNext_Click(System.Object sender, System.Web.UI.ImageClickEventArgs e)
		{
			try
			{
				PageIndex.Text = ((int.Parse(PageIndex.Text)) + 1).ToString();
				DataGrid_DataBind();
			}
			catch (Exception ex)
			{
				showException(ex.Message);
			}
		}
		
		private void btnPREV_Click(System.Object sender, System.Web.UI.ImageClickEventArgs e)
		{
			try
			{
				PageIndex.Text = ((int.Parse(PageIndex.Text)) - 1).ToString();
				DataGrid_DataBind();
			}
			catch (Exception ex)
			{
				showException(ex.Message);
			}
			
		}
		
		private void BtnGo_Click(System.Object sender, System.Web.UI.ImageClickEventArgs e)
		{
			ResourceManager rm = GetResourceManager();
			try
			{
				PageIndex.Text = Server.HtmlEncode(GotoPage.Text);
				DataGrid_DataBind();
			}
			catch (Exception)
			{
				showException(rm.GetString("MSG057"));
			}
			
		}
		
		private void btnSearch_Click(System.Object sender, System.EventArgs e)
		{
			try
			{
				PageIndex.Text = System.Convert.ToString(1);
				DataGrid_DataBind();
			}
			catch (Exception ex)
			{
				showException(ex.Message);
			}
			
		}
		
		private void Page_Load(System.Object sender, System.EventArgs e)
		{
			//在這裡放置使用者程式碼以初始化網頁
			try
			{
				
				if (!Page.IsPostBack)
				{
					
				}
				else
				{
					if (IsReSubmit)
					{
						DataGrid_DataBind();
					}
				}
			}
			catch (Exception ex)
			{
				showException(ex.Message);
			}
			this.BtnNext.Click+=BtnNext_Click;
            this.BtnGo.Click+=BtnGo_Click;
            this.btnPREV.Click+=btnPREV_Click;
            this.btnSearch.Click+=btnSearch_Click;
			
		}
		
		
		//將資料呈現於畫面
		public void DataGrid_Bind_VNDW0040(HtmlTable DataGrid, ArrayList ArrKey, DataTable DataSource)
		{
			
			HtmlTableRow tr = (HtmlTableRow) (DataGrid.FindControl("DataGrid_head"));
			int i = 0;
			int j = 0;
			int k = 0;
			string Url = "";
			string className = "";
			ResourceManager rm = GetResourceManager();
			try
			{
				for (i = 0; i <= DataSource.Rows.Count - 1; i++)
				{
					HtmlTableRow htr = new HtmlTableRow();
					if (i % 2 == 0)
					{
						className = "evenRow";
					}
					else
					{
						className = "oddRow";
					}
					htr.Attributes.Add("class", className);
					DataGrid.Rows.Add(htr);
					for (j = 0; j <= tr.Cells.Count - 1; j++)
					{
						HtmlTableCell hd = new HtmlTableCell();
						if (DataSource.Rows[i]["status"].ToString().Length != 0)
						{
							//P3511M0122-報價呈案程式修改案
							//判斷狀態為8則不可修改與刪除
							//If DataSource.Rows(i)("status").ToString.Substring(0, 1) = "8" Then
							//判斷狀態為3,4,5,6,7,8則不可修改與刪除
							if (DataSource.Rows[i]["status"].ToString().Substring(0, 1) == "3" || 
								DataSource.Rows[i]["status"].ToString().Substring(0, 1) == "4" || 
								DataSource.Rows[i]["status"].ToString().Substring(0, 1) == "5" || 
								DataSource.Rows[i]["status"].ToString().Substring(0, 1) == "6" || 
								DataSource.Rows[i]["status"].ToString().Substring(0, 1) == "7" || 
								DataSource.Rows[i]["status"].ToString().Substring(0, 1) == "8")
							{
								tr.Cells[1].Attributes["class"] = "else";
								tr.Cells[2].Attributes["class"] = "else";
							}
							else
							{
								tr.Cells[1].Attributes["class"] = "link";
								tr.Cells[2].Attributes["class"] = "link";
							}
						}
						else
						{
							//狀態為空則不可刪除
							tr.Cells[1].Attributes["class"] = "link";
							tr.Cells[2].Attributes["class"] = "else";
						}
						
						switch (tr.Cells[j].Attributes["class"].ToLower())
						{
							case "link":
								Url = string.Format("{0}?action_type={1}&GUID={2}", tr.Cells[j].Attributes["url"].ToString(), tr.Cells[j].ID, Guid.NewGuid());
								for (k = 0; k <= ArrKey.Count - 1; k++)
								{
									Url += "&" + ArrKey[k] + "=" + DataSource.Rows[i][ArrKey[k].ToString()].ToString();
								}
								
								hd.Width = tr.Cells[j].Width;
								hd.InnerHtml = string.Format("<a href=\"javascript:window.showModalDialog(\'{0}\',this,\'{1}\', true)\">{2}</a>", Url, "dialogWidth=800px;dialogHeight=740px;scrollbars=no;center=yes;border=thin;help=no;status=no;resizable=no;", rm.GetString(tr.Cells[j].ID));
								break;
							case "tdtitle":
								hd.Width = tr.Cells[j].Width;
								hd.Attributes.Add("class", "detail");
								hd.InnerText = System.Convert.ToString(DataSource.Rows[i][tr.Cells[j].Controls[1].ID.Replace("lbl", "")].ToString());
								break;
							default:
								hd.Width = tr.Cells[j].Width;
								hd.InnerText = rm.GetString(System.Convert.ToString(tr.Cells[j].ID));
								break;
						}
						DataGrid.Rows[DataGrid.Rows.Count - 1].Cells.Add(hd);
						
					}
				}
			}
			catch (Exception ex)
			{
				throw (ex);
			}
			
			
		}
		
		
	}
	
}
